package com.chenfan.mcn.component;

import com.chenfan.ccp.common.result.R;
import com.chenfan.code.generate.CodeGenerateClient;
import com.chenfan.code.generate.dto.CodeGenDTO;
import com.chenfan.code.generate.vo.CodeGenerateVO;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.config.UserVoContextHolder;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.utils.RpcUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 编号生成助手
 *
 * @author wulg
 * @date 2023/2/28
 */
@Component
public class CodeGenerateHelper {


    private static CodeGenerateClient codeGenerateClient;

    @Autowired
    public CodeGenerateHelper(CodeGenerateClient codeGenerateClient) {
        CodeGenerateHelper.codeGenerateClient = codeGenerateClient;
    }

    /**
     * 生成编号
     *
     * @param prefix
     * @return
     */
    public static String getCode(String prefix) {
        return getCode(prefix, null);
    }

    /**
     * 生成编号
     *
     * @param prefix
     * @return
     */
    public static String getCode(String prefix, String errorMsg) {
        CodeGenDTO codeGenDTO = new CodeGenDTO();
        codeGenDTO.setReceiptType(prefix);
        codeGenDTO.setTenantId(UserVoContextHolder.getTenantId());
        R<CodeGenerateVO> response = codeGenerateClient.genCode(codeGenDTO);
        if (response == null || response.getCode() != R.SUCCESS) {
            throw new BusinessException(McnErrorCode.GEN_CODE_FAIL.code(), StringUtils.defaultIfBlank(errorMsg, McnErrorCode.GEN_CODE_FAIL.message()));
        }
        return response.getObj().getCode();
    }

    /**
     * 获取唯一归档编号
     *
     * @param key
     * @return
     */
    public static String getUniqueArchivingCode(String key) {
        CodeGenerateVO codeGenerateVO = RpcUtil.getObjException(codeGenerateClient.generate(key, "GD"), "归档编码取号失败");
        return codeGenerateVO.getCode();
    }

    /**
     * 获取批量编号
     *
     * @param prefix
     * @param count
     * @return
     */
    public static List<String> getCodes(String prefix, Integer count) {
        return getCodes(prefix, count, null);
    }

    /**
     * 获取批量编号
     *
     * @param prefix
     * @param count
     * @param errorMsg
     * @return
     */
    public static List<String> getCodes(String prefix, Integer count, String errorMsg) {
        CodeGenDTO codeGenDTO = new CodeGenDTO();
        codeGenDTO.setReceiptType(prefix);
        codeGenDTO.setTenantId(UserVoContextHolder.getTenantId());
        codeGenDTO.setCount(count);
        R<List<CodeGenerateVO>> response = codeGenerateClient.genCodes(codeGenDTO);
        if (response == null || response.getCode() != R.SUCCESS) {
            throw new BusinessException(McnErrorCode.GEN_CODE_FAIL.code(), StringUtils.defaultIfBlank(errorMsg, McnErrorCode.GEN_CODE_FAIL.message()));
        }
        return response.getObj().stream().map(CodeGenerateVO::getCode).collect(Collectors.toList());
    }
}
